package bp.data;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.List;
import java.util.Collections;

public class DataManager implements Runnable {

	private List<List<Double>> input;
	private List<List<Double>> output;

	private String dataFile;

	public DataManager(String dataFile) {
		this.dataFile = dataFile;
		this.input = Collections
				.synchronizedList(new ArrayList<List<Double>>());
		this.output = Collections
				.synchronizedList(new ArrayList<List<Double>>());
		this.readFile();
	}

	@Override
	public void run() {
		this.readFile();
	}

	private void readFile() {
		File file = new File(dataFile);
		BufferedReader reader = null;
		try {
			reader = new BufferedReader(new FileReader(file));
			String tempString = null;

			while ((tempString = reader.readLine()) != null) {
				String[] strs = tempString.trim().split(":");
				this.getOutput().add(this.toList(strs[0].trim()));
				this.getInput().add(this.toList(strs[1].trim()));
			}

			reader.close();
		} catch (IOException e) {
			e.printStackTrace();
		} finally {
			if (reader != null) {
				try {
					reader.close();
				} catch (IOException e1) {
				}
			}
		}
	}

	private List<Double> toList(String str) {
		List<Double> r = new ArrayList<Double>();
		String[] sr = str.split("\\s+");
		for (String s : sr) {
			r.add(Double.parseDouble(s.trim()));
		}
		return r;
	}

	public int getInputRecordCount() {
		return this.input.get(0).size();
	}

	public int getOutputRecordCount() {
		return this.output.get(0).size();
	}

	public List<List<Double>> getInput() {
		return input;
	}

	public List<List<Double>> getOutput() {
		return output;
	}
}
